package com.foxitcloud.webpdf.common;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

import com.foxitcloud.webpdf.utils.FXStringUtils;

/**
 * @author shaojie_zhuo
 * @version 1.0
 * Configuration access class.
 */
public final class Config {
	protected static final Logger log = (Logger) LoggerFactory.getLogger(Config.class);
	public static HashMap<String, Object> SettingsDictionary = new HashMap<String,Object>();
	
	static
    {
		Properties prop = new Properties();
		InputStream  fis;
		try {
            fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("webpdf.properties");
            prop.load(fis);
            
            //缓存路径
            String cacheRoot = prop.getProperty(ConfigConstans.FOXIT_CACHE_ROOT_PATH_KEY);
            String defaultCacheRoot  = ConfigConstans.DEFAULT_FOXIT_CACHE_ROOT_PATH;
            if(!FXStringUtils.IsNullOrEmpty(cacheRoot)){
            	defaultCacheRoot = cacheRoot ;
            }
            SettingsDictionary.put(ConfigConstans.FOXIT_CACHE_ROOT_PATH_KEY, defaultCacheRoot);
            
            //缓存过期天数
            String cacheExpirationDays = prop.getProperty(ConfigConstans.FOXIT_CACHE_EXPIRATION_DAYS_KEY);
            int defaultExpirationDays = ConfigConstans.DEFAULT_FOXIT_CACHE_EXPIRATION_DAYS;
            if(!FXStringUtils.IsNullOrEmpty(cacheExpirationDays)){
            	defaultExpirationDays = Integer.parseInt(cacheExpirationDays);
            }
            SettingsDictionary.put(ConfigConstans.FOXIT_CACHE_EXPIRATION_DAYS_KEY, defaultExpirationDays);
            SettingsDictionary.put(ConfigConstans.FOXIT_WEB_READER_DLL_DIR, prop.getProperty(ConfigConstans.FOXIT_WEB_READER_DLL_DIR).trim());
            SettingsDictionary.put("storageS3FileLocalDir", prop.getProperty("storageS3FileLocalDir").trim());
            SettingsDictionary.put("ucxSigcodeUrl", prop.getProperty("ucxSigcodeUrl").trim());
            String property = prop.getProperty(ConfigConstans.FOXIT_SIGNATURELIST_MAX);
            if(property != null)
            {
            	 SettingsDictionary.put(ConfigConstans.FOXIT_SIGNATURELIST_MAX, Integer.parseInt(property.trim()));
            }
            SettingsDictionary.put(ConfigConstans.FONT_ROOT_PATH_KEY, prop.getProperty(ConfigConstans.FONT_ROOT_PATH_KEY).trim());
            property = prop.getProperty(ConfigConstans.DEFAULT_IMAGE_VERTICAL_DPI_KEY);
            if(property != null)
            {
            	 SettingsDictionary.put(ConfigConstans.DEFAULT_IMAGE_VERTICAL_DPI_KEY, Integer.parseInt(property.trim()));
            }
            property = prop.getProperty(ConfigConstans.DDEFAULT_IMAGE_HORIZONTAL_DPI_KEY);
            if(property != null)
            {
            	 SettingsDictionary.put(ConfigConstans.DDEFAULT_IMAGE_HORIZONTAL_DPI_KEY, Integer.parseInt(property.trim()));
            }
            property = prop.getProperty(ConfigConstans.MAX_IMAGE_WIDTH_KEY);
            if(property != null)
            {
            	 SettingsDictionary.put(ConfigConstans.MAX_IMAGE_WIDTH_KEY, Integer.parseInt(property.trim()));
            }
            property = prop.getProperty(ConfigConstans.MAX_IMAGE_HEIGHT_KEY);
            if(property != null)
            {
            	 SettingsDictionary.put(ConfigConstans.MAX_IMAGE_HEIGHT_KEY, Integer.parseInt(property.trim()));
            }
            property = prop.getProperty(ConfigConstans.MAX_PAGE_NUMBER_KEY);
            if(property != null)
            {
            	 SettingsDictionary.put(ConfigConstans.MAX_PAGE_NUMBER_KEY, Integer.parseInt(property.trim()));
            }
            SettingsDictionary.put(ConfigConstans.IMAGE_ZOOM_LIST_KEY, prop.getProperty(ConfigConstans.IMAGE_ZOOM_LIST_KEY));

            log.debug("Load Config Successfully...");
	    } catch (Exception e) {
			log.error(e.getMessage());
	    }
    }
	
	 /**
	  * Get configuration by key.
	 * @param keyName key
	 * @return configuration which specified by the key.
	 */
	 @SuppressWarnings("unchecked")
	 public static <T> T  tryGetValue(String keyName)
     {
		 Object value = SettingsDictionary.get(keyName);
		if(value == null)
			return null;
		
		return (T)value;
     }
	 
	 /**
	 * Get configuration by key.
	 * @param keyName key
	 * @param defaultValue default value.
	 * @return configuration which specified by the key. if the value is null, default value will be returned.
	 */
	 @SuppressWarnings("unchecked")
	 public static <T> T getValue(String keyName, T defaultValue)
     {
         T local =  (T)tryGetValue(keyName);
         if (local != null)
         {
             return (T)local;
         }
         return defaultValue;
     }
	 
	 /**
	 * Set configuration by key.
	 * @param keyName key
	 * @param value configuration which specified by the key.
	 */
	 public static void setValue(String keyName, Object value)
     {
         if (keyName == null)
         {
             throw new IllegalArgumentException("keyName");
         }
         SettingsDictionary.put(keyName, value);
     }
}
